package com.zmn.plat.admin.controller.orderconfig.orderstock.sp.warning;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.zmn.common.dto.VtDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.plat.common.dto.servcategory.ServCategoryDRO;
import com.zmn.plat.model.entity.orderconfig.orderstock.sp.SpWarningConfig;
import com.zmn.plat.model.entity.orderconfig.orderstock.sp.SpWarningConfigQuery;

/**
 * 服务商库存预警配置
 *
 * @author xiangjunming
 * @since 2020年7月22日16:49:40
 */
@Controller
@RequestMapping("/plat/sp/warning/config/")
public class SpWarningConfigListController extends SpWarningConfigBaseController {

    private Logger logger = LoggerFactory.getLogger(SpWarningConfigListController.class);

    /**
     * 描述: 区域列表
     *
     * @param query
     *            查询实体
     * @return ModelAndView
     */
    @SuppressWarnings("Duplicates")
    @RequestMapping(value = "list")
    @ReadOnlyConnection
    public ModelAndView list(@ModelAttribute SpWarningConfigQuery query) {
        ModelAndView mav = new ModelAndView("orderconfig/orderstock/sp/warning/list");
        if (Objects.isNull(query.getSpId())) {
            logger.error("服务商ID不能为空");
            return null;
        }

        // 城市集合
        List<VtDTO> cityList = baseCompanyAreaService.listVtByManageCompanyId(query.getSpId());

        // 服务分类集合
        List<ServCategoryDRO> servCategoryDROList =
            manageCompanyAreaBService.listServCategByManageCompanyIdAndCityId(query.getSpId(), query.getCityId());

        List<VtDTO> categoryOneDROList = Collections.emptyList();
        if (NumberUtil.isPositiveInteger(query.getServCategId())) {
            categoryOneDROList = manageCompanyAreaBService.listCategOneByManageCompanyIdAndServCategIdAndShowTypeAndCityId(query.getSpId(),
                query.getServCategId(), query.getCityId());
        }

        List<VtDTO> categoryTwoDROList = Collections.emptyList();
        if (NumberUtil.isPositiveInteger(query.getCategOneId())) {
            categoryTwoDROList = manageCompanyAreaBService.listCategTwoByManageCompanyIdAndServAndCategOneIdAndShowTypeAndCityId(
                query.getSpId(), query.getServCategId(), query.getCategOneId(), query.getCityId());
        }

        List<SpWarningConfig> items = spWarningConfigService.listPageByQuery(query);
        mav.addObject("items", items);
        mav.addObject("vo", query);
        mav.addObject("cityList", cityList);
        mav.addObject("servCategoryDROList", servCategoryDROList);
        mav.addObject("categoryOneDROList", categoryOneDROList);
        mav.addObject("categoryTwoDROList", categoryTwoDROList);
        return mav;
    }

    /**
     * 描述: 批量设置
     *
     * @param spId
     *            查询实体
     * @return ModelAndView
     */
    @RequestMapping(value = "batchSet")
    @ReadOnlyConnection
    public ModelAndView batchSet(Integer spId) {
        ModelAndView mav = new ModelAndView("orderconfig/orderstock/sp/warning/batchSet");
        // 城市集合
        List<VtDTO> cityList = baseCompanyAreaService.listVtByManageCompanyId(spId);
        mav.addObject("spId", spId);
        mav.addObject("cityList", cityList);
        return mav;
    }
}
